#include "PreCompile.hpp"
#include "SettingsTests.hpp"

namespace BuildNG
{
  SettingsTests::SettingsTests()
    : settings_file("TestFiles/TestSettingsFile.xml")
  {
  }

  void SettingsTests::test_get_setting_string_from_elem_content()
  {
    Glib::ustring res;
    bool get_res = settings_file.try_get_setting_string(
      "/test-settings/element-content-setting",res);
    assert(res == "element content");
    assert(get_res);

    // try the same thing another time, to make sure it works with a string that's not empty
    get_res = settings_file.try_get_setting_string(
      "/test-settings/element-content-setting",res);
    assert(res == "element content");
    assert(get_res == true);
    
    get_res = settings_file.try_get_setting_string(
      "/test-settings/non-existing-element",res);
    assert(get_res == false);

    try
    {
      // here, an exception will be thrown, because the problem was not that the 
      // setting was not defined, but that there's actually something wrong 
      // in the settings file.
      get_res = settings_file.try_get_setting_string(
        "/test-settings/provided-more-than-once",res);
      assert(!"an exception should have been thrown.");
    }
    catch(...)
    {
    }

    assert(settings_file.get_setting_string("/test-settings/element-content-setting") == "element content");

    try
    {
      settings_file.get_setting_string("/test-settings/non-existing-element");
      assert(!"an exception should have been thrown.");
    }
    catch(...)
    {
    }

    try
    {
      settings_file.get_setting_string("/test-settings/provided-more-than-once");
      assert(!"an exception should have been thrown.");
    }
    catch(...)
    {
    }
  }

  void SettingsTests::test_get_setting_string_from_attribute()
  {
    Glib::ustring res;
    bool get_res = settings_file.try_get_setting_string(
      "/test-settings/attr-setting-test/@teh-attr",res);
    assert(res == "the best setting evah");
    assert(get_res);

    // try the same thing another time, to make sure it works with a string that's not empty
    get_res = settings_file.try_get_setting_string(
      "/test-settings/attr-setting-test/@teh-attr",res);
    assert(res == "the best setting evah");
    assert(get_res == true);
    
    get_res = settings_file.try_get_setting_string(
      "/test-settings/attr-setting-test/@non-existing-attr",res);
    assert(get_res == false);

    try
    {
      // here, an exception will be thrown, because the problem was not that the 
      // setting was not defined, but that there's actually something wrong 
      // in the settings file.
      get_res = settings_file.try_get_setting_string(
        "/test-settings/attr-provided-more-than-once/@teh-attr",res);
      assert(!"an exception should have been thrown.");
    }
    catch(...)
    {
    }

    assert(settings_file.get_setting_string("/test-settings/attr-setting-test/@teh-attr") == "the best setting evah");

    try
    {
      settings_file.get_setting_string("/test-settings/attr-setting-test/@non-existing-attr");
      assert(!"an exception should have been thrown.");
    }
    catch(...)
    {
    }

    try
    {
      settings_file.get_setting_string("/test-settings/attr-setting-test/@non-existing-attr");
      assert(!"an exception should have been thrown.");
    }
    catch(...)
    {
    }
  }
}